package com.zsh.boot.controller;

import com.zsh.boot.bean.Person;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsh
 * @Description
 * @create 2022-02-19 15:48
 */

@RestController
public class ParamTestController {

    /**
     * 数据绑定，页面提交的数据（GET，POST）都可以和对象属性进行绑定
     * @param person
     * @return
     */
    @PostMapping("/saveuser")
    public Person saveUser(Person person){
        return person;
    }

    @GetMapping("/car/{id}/owner/{username}")
    public Map<String, Object> getCar(@PathVariable("id") Integer id,
                                      @PathVariable("username") String username,
                                      @PathVariable Map<String, String> pv,
                                      @RequestHeader("User-Agent") String userAgent,
                                      @RequestHeader Map<String, String> headers,
                                      @RequestParam("age") Integer age,
                                      @RequestParam("inters") List<String> inters,
                                      @RequestParam Map<String, String> params,
                                      @CookieValue("JSESSIONID") String JSESSIONID) {
            Map<String, Object> map = new HashMap<>();
            /*map.put("id", id);
            map.put("username",username);
            map.put("pv",pv);
            map.put("userAgent",userAgent);
            map.put("headers",headers);*/
            map.put("age",age);
            map.put("inters",inters);
            map.put("params",params);
            map.put("JSESSIONID",JSESSIONID);
//            map.put("cookie",cookie);
            return map;
    }

    @PostMapping(value = "/save") //存在URL乱码问题,JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。
    public Map<String, Object> postMethod(@RequestBody String content){

        Map<String, Object> map = new HashMap<>();
        map.put("content",content);

        return map;
    }

    //1.矩阵变量语法：/cars/sell;low=34;brand=byd,audi,yd
    //2.Spring Boot默认禁用矩阵变量 ，需
    // 要手动开启，原理：对于路径的处理，urlPathHelper 进行解析，
    // removeSemicolonContent （移除封号内容，默认true） 支持矩阵变量，
    // 需要配置类配置
    //3.矩阵变量必须有url路径变量才能被解析
    @GetMapping("/cars/{path}") //为什么不能写sell
    public Map<String, Object> carsSell(@MatrixVariable("low") Integer low,
                                        @MatrixVariable("brand") List<String> brand,
                                        @PathVariable("path") String path){

        Map<String, Object> map = new HashMap<>();
        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);

        return map;
    }

    //boss/1;age=20/2;age=20
    @GetMapping("/boss/{boosId}/{empId}")
    public Map<String, Object> boss(@MatrixVariable(value = "age",pathVar = "boosId") Integer boosAge,
                                    @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
        Map<String, Object> map = new HashMap<>();

        map.put("boosAge",boosAge);
        map.put("empAge",empAge);
        return map;
    }
}
